
/*
 * CTDR_2DField.cpp
 *
 *  Created on: 6 déc. 2010
 *      Author: mbenoit
 */

#include "CTDR_2DField.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include "global.h"
#include "TString.h"
#include "TH2D.h"
#include "TFile.h"

using namespace std;
CTDR_2DField::CTDR_2DField(double LX,double LY, double LZ,TString fEx, TString fEy,TString save) :
CTDR_Field(LX, LY, LZ) {

	ifstream ex(fEx);
	ifstream ey(fEy);


	int n;
	double x,y,E;
	n=GetLength(fEx);
	gEx = new TGraph2D(n);

	cout << TString::Format("[CTDR_Field] Reading Ex data file %s",fEx.Data()) << endl;
	for(int i=0;i<n;i++){

		ex >> x >> y >> E;
		//cout << x << " " << y << " " << E << endl;
		gEx->SetPoint(i,fabs(x),fabs(y),1e-4*E);
	};

	n=GetLength(fEy);
	gEy = new TGraph2D(n);

	cout << TString::Format("[CTDR_Field] Reading Ey data file %s",fEy.Data()) << endl;
	for(int i=0;i<n;i++){

		ey >> x >> y >> E;
		gEy->SetPoint(i,fabs(x),fabs(y),1e-4*E);
	};

	gEx->SetNpx(160);
	gEx->SetNpy(500);

	gEy->SetNpx(160);
	gEy->SetNpy(500);

	cout << TString::Format("[CTDR_Field] Extracting Ex Histogram") << endl;
	hEx=gEx->GetHistogram();
	cout << TString::Format("[CTDR_Field] Extracting Ey Histogram") << endl;
	hEy=gEy->GetHistogram();

	hEx->SetName("hEx");
	hEy->SetName("hEy");
	gEx->SetName("gEx");
	gEy->SetName("gEy");

	TFile *f = new TFile(save,"RECREATE");
	cout << TString::Format("[CTDR_Field] Writing histogram to file %s",save.Data()) << endl;

	hEx->Write();
	hEy->Write();
	gEx->Write();
	gEy->Write();


	f->Close();
	delete f;

	ex.close();
	ey.close();
}

CTDR_2DField::CTDR_2DField(double LX,double LY, double LZ,TString save) :
CTDR_Field(LX, LY, LZ) {

	cout << TString::Format("[CTDR_Field] Opening Efield histogram file %s",save.Data()) << endl;
	TFile *f =TFile::Open(save);

	//gEx= (TGraph2D*)f->FindObject("gEx");
	//gEy= (TGraph2D*)f->FindObject("gEy");

	cout << TString::Format("[CTDR_Field] Reading Ex Histogram") << endl;
	f->GetObject("hEx",hEx);
	cout << TString::Format("[CTDR_Field] Reading Ey Histogram") << endl;
	f->GetObject("hEy",hEy);



	//f->Close();
	//delete f;

}



int CTDR_2DField::GetLength(TString file){

	ifstream in(file);
	int n=0;
	double a,b,c;
	while(!in.eof())
	{	in >> a >> b >> c ;
		n++;
	};
	in.close();
	return n;

}

void  CTDR_2DField::SetChamp(double x, double y , double z){

// fix the constant !!
	if(z==zmax){
		Ex=-1e-4*hEx->Interpolate(Mod(x,Lx),z-0.5);
		Ey=0;
		Ez=0;
	}
	if(z<zmax && z>0){

		if(z>0 && z<zmax)Ex=-1e-4*hEx->Interpolate(Mod(x,this->LX),z);
		if(z>0 && z<zmax)Ez=-1e-4*hEy->Interpolate(Mod(x,this->LX),z);
	Ey=0;
	}
	else{
		Ex=0;
		Ey=0;
		Ez=0;
		};
	//cout << "x=" << x << " y= " << y << endl;
	//cout << "Ex=" << Ex << " Ey= " << Ey << endl;

}


double CTDR_2DField::Mod(double x, double L){

	double scale=fabs(x/L);
	if(x>=0) return x-L*floor(scale);
	else return L-(fabs(x)-L*floor(scale));

}

CTDR_2DField::~CTDR_2DField() {
	// TODO Auto-generated destructor stub
}
